package icu.qx.wm.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import icu.qx.wm.domain.WmReturnMaterialList;
import icu.qx.wm.dto.MaterialReturnDTO;
import icu.qx.wm.vo.MaterialListVo;
import icu.qx.wm.vo.ProWorkOrderVo;
import icu.qx.wm.vo.ProWorkTaskVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import icu.qx.wm.domain.WmProductionMaterialReturn;
import icu.qx.wm.service.IWmProductionMaterialReturnService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 生产退料Controller
 *
 * @author alize
 * &#064;date  2024-06-26
 */
@RestController
@RequestMapping("/wm/inbound/material_return")
public class WmProductionMaterialReturnController extends BaseController {
    private final IWmProductionMaterialReturnService wmProductionMaterialReturnService;

    @Autowired
    public WmProductionMaterialReturnController(IWmProductionMaterialReturnService wmProductionMaterialReturnService) {
        this.wmProductionMaterialReturnService = wmProductionMaterialReturnService;
    }

    /**
     * 查询生产退料列表
     */
    @RequiresPermissions("wm:inbound:material_return:list")
    @GetMapping("/list")
    public TableDataInfo list(WmProductionMaterialReturn wmProductionMaterialReturn) {
        startPage();
        List<WmProductionMaterialReturn> list = wmProductionMaterialReturnService.selectWmProductionMaterialReturnList(wmProductionMaterialReturn);
        return getDataTable(list);
    }

    /**
     * 导出生产退料列表
     */
    @RequiresPermissions("wm:inbound:material_return:export")
    @Log(title = "生产退料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WmProductionMaterialReturn wmProductionMaterialReturn) {
        List<WmProductionMaterialReturn> list = wmProductionMaterialReturnService.selectWmProductionMaterialReturnList(wmProductionMaterialReturn);
        ExcelUtil<WmProductionMaterialReturn> util = new ExcelUtil<>(WmProductionMaterialReturn.class);
        util.exportExcel(response, list, "生产退料数据");
    }

    /**
     * 获取生产退料详细信息
     */
    @RequiresPermissions("wm:inbound:material_return:query")
    @GetMapping(value = "/{pmrCode}")
    public AjaxResult getInfo(@PathVariable("pmrCode") String pmrCode) {
        return success(wmProductionMaterialReturnService.selectWmProductionMaterialReturnByPmrCode(pmrCode));
    }

    /**
     * 新增生产退料
     */
    @RequiresPermissions("wm:inbound:material_return:add")
    @Log(title = "生产退料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WmProductionMaterialReturn wmProductionMaterialReturn) {
        return toAjax(wmProductionMaterialReturnService.insertWmProductionMaterialReturn(wmProductionMaterialReturn));
    }

    /**
     * 修改生产退料
     */
    @RequiresPermissions("wm:inbound:material_return:edit")
    @Log(title = "生产退料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WmProductionMaterialReturn wmProductionMaterialReturn) {
        return toAjax(wmProductionMaterialReturnService.updateWmProductionMaterialReturn(wmProductionMaterialReturn));
    }

    /**
     * 删除生产退料
     */
    @RequiresPermissions("wm:inbound:material_return:remove")
    @Log(title = "生产退料", businessType = BusinessType.DELETE)
    @DeleteMapping("/{pmrCodes}")
    public AjaxResult remove(@PathVariable String[] pmrCodes) {
        return toAjax(wmProductionMaterialReturnService.deleteWmProductionMaterialReturnByPmrCodes(pmrCodes));
    }

    // 查询工艺路线
    @GetMapping("/route/code")
    public AjaxResult getRouteCode() {
        return success(wmProductionMaterialReturnService.selectRouteCode());
    }

    // 查询工序
    @GetMapping("/procedure/code")
    public AjaxResult getProcedureCode() {
        return success(wmProductionMaterialReturnService.selectProcedureCode());
    }

    // 查询物料清单
    @GetMapping("/material/list")
    public AjaxResult getMaterialList(WmReturnMaterialList wmReturnMaterialList) {
        return success(wmProductionMaterialReturnService.selectWmReturnMaterialList(wmReturnMaterialList));
    }

    // 查询班组列表
    @GetMapping("/work_group/list")
    public AjaxResult getPbsWorkGroupList() {
        return success(wmProductionMaterialReturnService.selectPbsWorkGroupList());
    }

    // 查询生产工单
    @RequiresPermissions("wm:inbound:material_return:list")
    @GetMapping("/work_order/list")
    public TableDataInfo getWorkOrderList(ProWorkOrderVo proWorkOrderVo) {
        startPage();
        List<ProWorkOrderVo> list = wmProductionMaterialReturnService.selectWorkOrder(proWorkOrderVo);
        return getDataTable(list);
    }

    // 查询工单任务
    @RequiresPermissions("wm:inbound:material_return:list")
    @GetMapping("/work_task/list")
    public TableDataInfo getProWorkTaskList(ProWorkTaskVo proWorkTaskVo) {
        startPage();
        List<ProWorkTaskVo> list = wmProductionMaterialReturnService.selectProWorkTaskList(proWorkTaskVo);
        return getDataTable(list);
    }

    // 查询任务负责人
    @RequiresPermissions("wm:inbound:material_return:list")
    @GetMapping("/task_manager/list")
    public AjaxResult getTaskManager() {
        return success(wmProductionMaterialReturnService.selectTaskManager());
    }

    // 查找物料
    @RequiresPermissions("wm:inbound:material_return:query")
    @GetMapping("/material_list/get")
    public TableDataInfo getMaterialList(MaterialListVo materialListVo) {
        startPage();
        List<MaterialListVo> list = wmProductionMaterialReturnService.selectMaterialList(materialListVo);
        return getDataTable(list);
    }

    // 查找单位
    @RequiresPermissions("wm:inbound:material_return:list")
    @GetMapping("/unit/list")
    public AjaxResult getUnitList() {
        return success(wmProductionMaterialReturnService.selectUnitList());
    }

    // 退料单入库
    @RequiresPermissions("wm:inbound:material_return:store")
    @Log(title = "生产退料单", businessType = BusinessType.UPDATE)
    @PutMapping("/store")
    public AjaxResult store(@RequestBody MaterialReturnDTO materialReturnDTO) {
        return toAjax(wmProductionMaterialReturnService.storeMaterialReturn(materialReturnDTO));
    }
}
